using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;

namespace TanashCMS.Business
{
    public class WebsiteOption  : CMSDataObject
    {
        private int m_websiteID = 0;
        private string m_OptionTitle, m_OptionValue;
        public virtual string OptionTitle { get { return m_OptionTitle; } set { m_OptionTitle = value; } }
        public virtual string OptionValue { get { return m_OptionValue; } set { m_OptionValue = value; } }
        public virtual int WebsiteID { get { return m_websiteID; } set { m_websiteID = value; } }

        public static List<WebsiteOption> List(int WebsiteID)
        {
            return (List<WebsiteOption>)Configuration.GetCurrentSession().CreateQuery("select o from WebsiteOption o where o.WebsiteID = " + WebsiteID.ToString() + " order by o.OptionTitle").List<WebsiteOption>();
        }

        public static void Update(WebsiteOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            WebsiteOption originalOption = session.Get<WebsiteOption>(option.ID);

            List<WebsiteOption> exists = (List<WebsiteOption>)session.CreateQuery("select o from WebsiteOption o where o.WebsiteID = " + option.WebsiteID.ToString() + " and o.OptionTitle = '" + option.OptionTitle + "' and o.ID != " + option.ID.ToString()).List<WebsiteOption>();

            if (exists.Count > 0)
                return;

            originalOption.OptionTitle = option.OptionTitle;
            originalOption.OptionValue = option.OptionValue;
            try
            {
                session.SaveOrUpdate(originalOption);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(WebsiteOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            List<WebsiteOption> exists = (List<WebsiteOption>)session.CreateQuery("select o from WebsiteOption o where o.WebsiteID = " + option.WebsiteID.ToString() + " and o.OptionTitle = '" + option.OptionTitle + "'").List<WebsiteOption>();

            if (exists.Count > 0)
                return;

            try
            {
                session.Save(option);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Delete(WebsiteOption option)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            WebsiteOption originalOption = session.Get<WebsiteOption>(option.ID);
            try
            {
                session.Delete(originalOption);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
    }
}
